"""
用户视图
最后修改：3.36
修改人：陈泯全
"""
from flask import Blueprint, request, current_app
import json
from service.UserService import userService

bp = Blueprint("UserSubSys", __name__, url_prefix="/UserSubSys")
"""
专家用户申请
"""


@bp.route('/ApplyExpert', methods=['GET', 'POST'])
def applyExpert():
    if request.method == 'POST':
        byte = request.data
        data = byte.decode(encoding="utf-8")
        try:
            dic = json.loads(data)
            name = dic['name']
            degreee = dic['degree']
            pic1 = dic['pic1']
            pic2 = dic['pic2']
            pic3 = dic['pic3']
            userId = dic['userid']
        except Exception as r:
            current_app.logger.info('专家申请接口json参数错误')
            return {"status": "0"}
        service = userService()
        out = service.applyExpert(name, degreee, userId, pic1, pic2, pic3)
        if out == 1:
            return {"status": "1"}
        else:
            return {"status": "0"}


"""
修改密码
"""


@bp.route('/ChangePassward', methods=['GET', 'POST'])
def changePassward():
    if request.method == 'POST':
        uid = request.form.get("userid", type=str, default=None)
        oldPassward = request.form.get("oldpassward", type=str, default=None)
        newPassward1 = request.form.get("newpassward1", type=str, default=None)
        newPassward2 = request.form.get("newpassward2", type=str, default=None)
        service = userService()
        out = service.changePassward(uid, oldPassward, newPassward1, newPassward2)
        if out == 1:
            return {"status": "1"}
        else:
            return {"status": "0"}


"""
修改手机号码
"""


@bp.route('/ChangePhone', methods=['GET', 'POST'])
def changePhone():
    if request.method == 'POST':
        uid = request.form.get("userid", type=str, default=None)
        passward = request.form.get('passward', type=str, default=None)
        phone = request.form.get('phone', type=str, default=None)
        service = userService()
        out = service.changePhone(uid, passward, phone)
        if out == 1:
            return {"status": "1"}
        else:
            return {"status": "0"}


"""
修改用户名
"""


@bp.route('/ChangeUserName', methods=['GET', 'POST'])
def changeUserName():
    if request.method == 'POST':
        uid = request.form.get("userid", type=str, default=None)
        username = request.form.get('username', type=str, default=None)
        service = userService()
        out = service.changeUserName(uid, username)
        if out == 1:
            return {"status": "1"}
        else:
            return {"status": "0"}


"""
修改邮箱
"""


@bp.route('/ChangeMail', methods=['GET', 'POST'])
def changeMail():
    if request.method == 'POST':
        uid = request.form.get("userid", type=str, default=None)
        mail = request.form.get('mail', type=str, default=None)
        varifycode = request.form.get('varifycode', type=str, default=None)
        service = userService()
        out = service.changeMail(uid, mail, varifycode)
        if out == 1:
            return {"status": "1"}
        else:
            return {"status": "0"}


"""
注册
"""


@bp.route('/Register', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        username = request.form.get('username', type=str, default=None)
        passward = request.form.get('passward', type=str, default=None)
        phone = request.form.get('phone', type=str, default=None)
        mail = request.form.get('mail', type=str, default=None)
        varifycode = request.form.get('verificationCode', type=str, default=None)
        service = userService()
        out = service.register(username, passward, phone, mail, varifycode)
        if out == 1:
            return {"status": "1"}
        else:
            return {"status": "0"}


"""
登陆
"""


@bp.route('/Login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        phone = request.form.get('phone', type=str, default=None)
        passward = request.form.get('passward', type=str, default=None)
        varifiycode = request.form.get('verificationCode', type=str, default=None)
        service = userService()
        status, userId, identify = service.login(phone, passward, varifiycode)
        return {
            "status": status,
            "userid": userId,
            "identify": identify
        }


"""
获取验证码
"""


@bp.route('/GetVarifyCode', methods=['GET', 'POST'])
def getVarifyCode():
    if request.method == 'GET':
        service = userService()
        out = service.getVarifyCode()
        if out == 1:
            return {
                "status": "1",
                "GetVarifyCode": "1234"
            }
        else:
            return {
                "status": "0",
                "GetVarifyCode": "1234"
            }


"""
测试
"""


@bp.route('/', methods=['GET', 'POST'])
def index():
    # #测试图片,编码有关的代码
    # if request.method == 'POST':
    #     file_obj = request.data
    #     byte = base64.b64decode(file_obj)
    #     string = byte.decode(encoding="utf-8")
    #     a = json.loads(string)
    #     print(a)
    return {
        "stuatus": 'ok',
    }
